package com.masterclient.app.modules.aftersale;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eoner.baselibrary.bean.aftersale.AfterSalePurchaseItemBean;
import com.eoner.baselibrary.bean.aftersale.AfterSalePurchaseListData;
import com.eoner.baselibrary.bean.purchase.PurchaseSellerBean;
import com.eoner.baselibrary.utils.IntentUtil;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.managerlibrary.router.AroutePath;
import com.eoner.managerlibrary.sobot.SobotManager;
import com.masterclient.app.BaseActivity;
import com.masterclient.app.R;
import com.masterclient.app.base.ConfirmDialog;
import com.masterclient.app.modules.aftersale.adapter.AfterSalePurchaseListAdapter;
import com.masterclient.app.modules.aftersale.contract.AfterSalePurchaseListContract;
import com.masterclient.app.modules.aftersale.contract.AfterSalePurchaseListPresenter;
import com.masterclient.app.widget.TitlebarView;
import com.sobot.chat.SobotApi;
import com.sobot.chat.api.enumtype.SobotChatTitleDisplayMode;
import com.sobot.chat.api.model.Information;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @auther hjian
 * @date 2020/9/28.
 */
@Route(path = AroutePath.AFTER_SALE_PURCHASE_LIST_ACTIVITY)
public class AfterSalePurchaseListActivity extends BaseActivity<AfterSalePurchaseListPresenter> implements AfterSalePurchaseListContract.View {

    @BindView(R.id.title_bar)
    TitlebarView titleBar;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.swipe_layout)
    SwipeRefreshLayout swipeLayout;

    private AfterSalePurchaseListAdapter listAdapter;
    private int page;
    private View emptyView;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_refresh);
        ButterKnife.bind(this);

        titleBar.setTitle("售后退款");
        titleBar.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() { }
        });

        recyclerView.setPadding(0, ScreenUtils.dp2px(10),0,ScreenUtils.dp2px(20));

        emptyView = View.inflate(this,R.layout.null_order,null);
        ((TextView)emptyView.findViewById(R.id.tv_tip)).setText("还没发现你有售后服务哦～");

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        listAdapter = new AfterSalePurchaseListAdapter();
        listAdapter.bindToRecyclerView(recyclerView);
        listAdapter.setEmptyView(emptyView);
        emptyView.setVisibility(View.GONE);

        refreshData(1);

        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshData(1);
            }
        });

        listAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                refreshData(page);
            }
        },recyclerView);
        listAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                AfterSalePurchaseItemBean bean = listAdapter.getData().get(position);
                ARouter.getInstance().build(AroutePath.AFTER_SALE_PURCHASE_DETAIL_ACTIVITY)
                        .withString("order_product_id",bean.getOrder_product_id())
                        .withString("order_id", bean.getOrder_id()).navigation();
            }
        });

        listAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                PurchaseSellerBean bean = listAdapter.getData().get(position).getSeller();
                switch (view.getId()){
                    case R.id.tv_contact_seller:
                        if (TextUtils.isEmpty(bean.getMobile())) {
                            Information info_seller = SobotManager.getBaseInfo();
                            SobotApi.setChatTitleDisplayMode(mContext, SobotChatTitleDisplayMode.Default, "");
                            if (!TextUtils.isEmpty(bean.getSobot_app_key())) {
                                info_seller.setAppkey(bean.getSobot_app_key());
                                //info_seller.setInitModeType(4);
                                SobotManager.setSobotFlow(mContext, bean.getSobot_flow_company_id(),
                                        bean.getSobot_flow_group_id(), "0");
                            }
                            if (listAdapter.getData().get(position) != null) {
                                AfterSalePurchaseItemBean itemBean = listAdapter.getData().get(position);
                                Map<String, String> customInfo = new HashMap<String, String>();
                                customInfo.put("aftersale_no", itemBean.getRefund_no());
                                customInfo.put("aftersale_status", itemBean.getStatus());
                                info_seller.setCustomInfo(customInfo);
                                int totalFee = (int) Float.valueOf(itemBean.getRefund_amount()).floatValue();
                                String productName = "";
                                String productImageeUrl = "";
                                if (itemBean.getProducts().size() > 0) {
                                    productName = itemBean.getProducts().get(0).getName();
                                    productImageeUrl = itemBean.getProducts().get(0).getImage();
                                }
                                SobotManager.setOrderCardInfo(info_seller, itemBean.getRefund_no(), itemBean.getStatus(), totalFee
                                        , String.valueOf(itemBean.getProducts().size()), itemBean.getCreate_at(), productName, productImageeUrl);

                            }
                            SobotApi.startSobotChat(AfterSalePurchaseListActivity.this, info_seller);
                        }else {
                            ConfirmDialog dialog = new ConfirmDialog(mContext,"是否拨打电话",null,bean.getMobile(),"取消","确定","#FF6809");
                            dialog.setOnChooseListerner(new ConfirmDialog.OnChooseListerner() {
                                @Override
                                public void cancel() {
                                    dialog.dismiss();
                                }

                                @Override
                                public void confirm() {
                                    IntentUtil.toPhoneApp(mContext,bean.getMobile());
                                    dialog.dismiss();
                                }
                            });
                            dialog.show();
                        }
                        break;
                }
            }
        });

    }

    private void refreshData(int page){
        this.page = page;
        mPresenter.getAfterSaleListBean(page);
    }


    @Override
    public void showData(AfterSalePurchaseListData bean) {

        if (swipeLayout.isRefreshing()){
            swipeLayout.setRefreshing(false);
        }
        listAdapter.loadMoreComplete();

        if (page == 1){
            listAdapter.setNewData(bean.getItems());
            emptyView.setVisibility(bean.getItems() == null
                    || bean.getItems().size() == 0 ?View.VISIBLE:View.GONE);
        }else {
            listAdapter.addData(bean.getItems());
        }


        if(bean.getItems() == null || bean.getItems().size()<1 || !bean.isHas_more()){
            listAdapter.setEnableLoadMore(false);
        }else {
            ++page;
        }

    }

    @Override
    public void showFailure(String msg) {
        ToastUtil.showToast(msg);
        if (swipeLayout.isRefreshing()){
            swipeLayout.setRefreshing(false);
        }
        listAdapter.loadMoreComplete();
    }
}
